#pragma once
#include "Protocol.hpp"
#include <memory>
#include <unordered_map>
#include <functional>
#include "Comm.hpp"

using func_cal = std::function<int(int, int)>;
static const std::string opers = "+-*/%";       //验证这个static TODO
class Calculate
{
public:
    Calculate()
    {
        _funcs['+'] = [](int x, int y) { return x + y;};
        _funcs['-'] = [](int x, int y) { return x - y;};
        _funcs['*'] = [](int x, int y) { return x * y;};
        _funcs['/'] = [](int x, int y) { return x / y;};
        _funcs['%'] = [](int x, int y) { return x % y;};
    }
    std::shared_ptr<Response> Cal(std::shared_ptr<Request> req_ptr)
    {
        // int x = req_ptr->Getx();
        // int y = req_ptr->Gety();
        // char oper = req_ptr->Getoper();
        // if(y == 0)
        // {
        //     if(oper == '/')
        //         return Factory().BulidResponse(-1, div_err);
        //     else if(oper == '%')
        //         return Factory().BulidResponse(-1, mod_err);
        // }
        // if(opers.find(oper) == std::string::npos)
        //     return Factory().BulidResponse(-1, oper_err);
        // return Factory().BulidResponse(_funcs[oper](x, y), 0);
        int x = req_ptr->Getx();
        int y = req_ptr->Gety();
        char oper = req_ptr->Getoper();
        int state = 0;
        if(y == 0)
        {
            if(oper == '/') state = div_err;
            else if(oper == '%') state = mod_err;
        }
        if(opers.find(oper) == std::string::npos) state = oper_err;
        std::cout << "Cal::state: " << state << std::endl;
        return Factory().BulidResponse(_funcs[oper](x, y), state);
    }
private:
    std::unordered_map<char, func_cal> _funcs;
};